package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

/*
客服类:
专门和用户打交道的
*/
public abstract class BaseStudentController {
    private StudentService studentService = new StudentService();
     Scanner sc = new Scanner(System.in);
    //学生主菜单
    public final void start() {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    //System.out.println("添加");
                    break;
                case "2":
                    deleteStudent();
                    //System.out.println("删除");
                    break;
                case "3":
                    updateStudent();
                    // System.out.println("修改");
                    break;
                case "4":
                    findAllStudent();
                    //System.out.println("查询");
                    break;
                case "5":
                    System.out.println("退出");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入:");
                    break;
            }
        }

    }

    //修改学生
    public final void updateStudent() {
        String id;
        Student[] stu = studentService.findAllStudent();
        if (stu == null) {
            System.out.println("查无信息,请添加后操作");
            return;
        }
        id = inputStudentId();
        //id = inputStudentId(true);
        Student student = getStudent(id);
        studentService.updateStudent(student, id);
        System.out.println("修改成功");
    }

    //封装学生对象
    public abstract Student getStudent(String id) ;

    //判断学号是否存在
    public final String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生学号");
            id = sc.next();
            boolean exists = studentService.isExists(id);
            if (exists) {
                break;
            } else {
                System.out.println("您输入的学号不存在,请重新输入");
            }
        }
        return id;
    }
   //删除学生
    public final void deleteStudent() {
        String id;
        Student[] stu = studentService.findAllStudent();
        if (stu == null) {
            System.out.println("查无信息,请添加后操作");
            return;
        }
        //sid = inputStudentId(true);
        id = inputStudentId();
        studentService.deleteStudent(id);
        System.out.println("删除成功");
    }

    //查询学生
    public final void findAllStudent() {
        Student[] stu = studentService.findAllStudent();
        if (stu == null) {
            System.out.println("查无信息,请添加后操作");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < stu.length; i++) {
                if (stu[i] != null) {
                    System.out.println(stu[i].getId() + "\t" + stu[i].getName() + "\t" + stu[i].getAge() + "\t\t" + stu[i].getBirthday());

                }
            }
        }

    }

    //添加学生
    public final void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学号:");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {
                System.out.println("您输入的学号已存在,请重新输入");
            } else {
                break;
            }
        }
        Student student = getStudent(id);

        boolean flag = studentService.addStudent(student);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }
}
